﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Dal;
using HiStaff.Domain.Sys;
using HiStaff.Util.Enum;
using System.Collections;
using HiStaff.Domain.PA;
using HiStaff.Domain.HU;

namespace HiStaff.iProfile
{
    public partial class Allowance : FormBase
    {
        public Allowance()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.AddBasicCommandBar();
            var lstData = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>((DataTable)cboAllowance.DataSource);
            grdData.RootTable.Columns["ALLOWANCEID"].ValueList.PopulateValueList(lstData, "ID", "NAME");
            return base.FormLoad();
        }
        private void LoadEmployee()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = hiCheckBox1.Value });
                grdEmp.DataSource = lstEmployee;
                grdEmp.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void BindData()
        {
            try
            {
                if (grdEmp.SelectedItems.Count > 0 && grdEmp.SelectedItems[0].RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    this.Cursor = Cursors.WaitCursor;
                    var lstData = OracleHelper.ExcuteSelectMultiObject<PA_ALLOWANCE>("APRS_PA_ALLOWANCE", new PA_ALLOWANCE
                    {
                        EMPLOYEEID = ((HU_EMPLOYEE)grdEmp.SelectedItems[0].GetRow().DataRow).ID
                    });
                    grdData.DataSource = lstData;
                    grdData.Refresh();
                }
                else
                {
                    grdData.DataSource = null;
                    grdData.Refresh();

                    ResetInputControl(this);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void trvOrg_AfterSelect(object sender, TreeViewEventArgs e)
        {
            LoadEmployee();
        }

        private void grdEmp_SelectionChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void grdData_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGridToForm(grdData, this);
        }

        protected override bool SaveButtonClick()
        {
            var objSave = new PA_ALLOWANCE();
            ValueControlsToObject(objSave, this);

            foreach (var itm in cboEmployee.EmployeeSelected)
            {
                objSave.EMPLOYEEID = itm.ID;
                objSave.ID = null;
                OracleHelper.ExcuteNonQuery("APRU_PA_ALLOWANCE", objSave);
            }
            MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            BindData();
            return true;
        }
        protected override bool DeleteButtonClick()
        {
            if (grdData.SelectedItems.Count > 0 && grdData.SelectedItems[0].RowType == Janus.Windows.GridEX.RowType.Record)
            {
                var objDel = (PA_ALLOWANCE)grdData.SelectedItems[0].GetRow().DataRow;
                if (OracleHelper.ExcuteNonQuery("APRD_PA_ALLOWACE", objDel))
                {
                    MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindData();
                    return true;
                }
            }
            return false;
        }
        protected override bool CancelButtonClick()
        {
            BindDataFromGridToForm(grdData, this);
            return true;
        }

        private void hiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            LoadEmployee();
        }
    }
}
